MAXREFDES82# Code Documentation  V01.00
Maxim Smart Force Sensor
 All Files Functions Variables Macros Groups Pages
usbd_conf.c
Go to the documentation of this file.
1 
28 /* Includes ------------------------------------------------------------------*/
29 #include "stm32f4xx_hal.h"
30 #include "usbd_core.h"
31 
32 /* Private typedef -----------------------------------------------------------*/
33 /* Private define ------------------------------------------------------------*/
34 /* Private macro -------------------------------------------------------------*/
35 /* Private variables ---------------------------------------------------------*/
36 PCD_HandleTypeDef hpcd;
37 
38 /* Private function prototypes -----------------------------------------------*/
39 /* Private functions ---------------------------------------------------------*/
40 
41 /*******************************************************************************
42  PCD BSP Routines
43 *******************************************************************************/
49 void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
50 {
51  GPIO_InitTypeDef GPIO_InitStruct;
52 
53  if(hpcd->Instance == USB_OTG_FS)
54  {
55  /* Configure USB FS GPIOs */
56  __HAL_RCC_GPIOA_CLK_ENABLE();
57 
58  /* Configure DM DP Pins */
59  GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
60  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
61  GPIO_InitStruct.Pull = GPIO_NOPULL;
62  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
63  GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
64  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
65 
66  /* Configure VBUS Pin */
67  GPIO_InitStruct.Pin = GPIO_PIN_9;
68  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
69  GPIO_InitStruct.Pull = GPIO_NOPULL;
70  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
71 
72  /* Configure ID pin */
73  GPIO_InitStruct.Pin = GPIO_PIN_10;
74  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
75  GPIO_InitStruct.Pull = GPIO_PULLUP;
76  GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
77  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
78 
79  /* Enable USB FS Clocks */
80  __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
81 
82  /* Set USBFS Interrupt priority to 6 */
83  HAL_NVIC_SetPriority(OTG_FS_IRQn, 6, 0);
84 
85  /* Enable USBFS Interrupt */
86  HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
87  }
88 
89  else if(hpcd->Instance == USB_OTG_HS)
90  {
91 #ifdef USE_USB_HS_IN_FS
92 
93  /* Configure USB FS GPIOs */
94  __HAL_RCC_GPIOB_CLK_ENABLE();
95 
96  /*Configure GPIO for HS on FS mode*/
97  GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_14 |GPIO_PIN_15;
98  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
99  GPIO_InitStruct.Pull = GPIO_NOPULL;
100  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
101  GPIO_InitStruct.Alternate = GPIO_AF12_OTG_HS_FS;
102  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
103 
104  /* Configure VBUS Pin */
105  GPIO_InitStruct.Pin = GPIO_PIN_13;
106  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
107  GPIO_InitStruct.Pull = GPIO_NOPULL;
108  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
109  GPIO_InitStruct.Pull = GPIO_NOPULL;
110 
111 #else
112 
113  /* Configure USB HS GPIOs */
114  __HAL_RCC_GPIOA_CLK_ENABLE();
115  __HAL_RCC_GPIOB_CLK_ENABLE();
116  __HAL_RCC_GPIOC_CLK_ENABLE();
117  __HAL_RCC_GPIOH_CLK_ENABLE();
118  __HAL_RCC_GPIOI_CLK_ENABLE();
119 
120  /* CLK */
121  GPIO_InitStruct.Pin = GPIO_PIN_5;
122  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
123  GPIO_InitStruct.Pull = GPIO_NOPULL;
124  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
125  GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
126  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
127 
128  /* D0 */
129  GPIO_InitStruct.Pin = GPIO_PIN_3;
130  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
131  GPIO_InitStruct.Pull = GPIO_NOPULL;
132  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
133  GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
134  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
135 
136  /* D1 D2 D3 D4 D5 D6 D7 */
137  GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5 |\
138  GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
139  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
140  GPIO_InitStruct.Pull = GPIO_NOPULL;
141  GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
142  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
143 
144  /* STP */
145  GPIO_InitStruct.Pin = GPIO_PIN_0;
146  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
147  GPIO_InitStruct.Pull = GPIO_NOPULL;
148  GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
149  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
150 
151  /* NXT */
152  GPIO_InitStruct.Pin = GPIO_PIN_4;
153  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
154  GPIO_InitStruct.Pull = GPIO_NOPULL;
155  GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
156  HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
157 
158  /* DIR */
159  GPIO_InitStruct.Pin = GPIO_PIN_11;
160  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
161  GPIO_InitStruct.Pull = GPIO_NOPULL;
162  GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
163  HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
164 
165  /* Enable embedded ULPI */
166  __HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();
167 #endif /* USE_USB_HS_IN_FS */
168 
169  /* Enable USB HS Clocks */
170  __HAL_RCC_USB_OTG_HS_CLK_ENABLE();
171 
172  /* Set USBHS Interrupt priority to 6 */
173  HAL_NVIC_SetPriority(OTG_HS_IRQn, 6, 0);
174 
175  /* Enable USBHS Interrupt */
176  HAL_NVIC_EnableIRQ(OTG_HS_IRQn);
177  }
178 }
179 
185 void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
186 {
187  if(hpcd->Instance == USB_OTG_FS)
188  {
189  /* Disable USB FS Clocks */
190  __HAL_RCC_USB_OTG_FS_CLK_DISABLE();
191  __HAL_RCC_SYSCFG_CLK_DISABLE();
192  }
193  else if(hpcd->Instance == USB_OTG_HS)
194  {
195  /* Disable USB FS Clocks */
196  __HAL_RCC_USB_OTG_HS_CLK_DISABLE();
197  __HAL_RCC_SYSCFG_CLK_DISABLE();
198  }
199 }
200 
201 /*******************************************************************************
202  LL Driver Callbacks (PCD -> USB Device Library)
203 *******************************************************************************/
204 
205 
211 void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
212 {
213  USBD_LL_SetupStage(hpcd->pData, (uint8_t *)hpcd->Setup);
214 }
215 
222 void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
223 {
224  USBD_LL_DataOutStage(hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
225 }
226 
233 void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
234 {
235  USBD_LL_DataInStage(hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
236 }
237 
243 void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
244 {
245  USBD_LL_SOF(hpcd->pData);
246 }
247 
253 void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
254 {
255  USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
256 
257  /* Set USB Current Speed */
258  switch(hpcd->Init.speed)
259  {
260  case PCD_SPEED_HIGH:
261  speed = USBD_SPEED_HIGH;
262  break;
263 
264  case PCD_SPEED_FULL:
265  speed = USBD_SPEED_FULL;
266  break;
267 
268  default:
269  speed = USBD_SPEED_FULL;
270  break;
271  }
272  USBD_LL_SetSpeed(hpcd->pData, speed);
273 
274  /* Reset Device */
275  USBD_LL_Reset(hpcd->pData);
276 }
277 
283 void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
284 {
285  USBD_LL_Suspend(hpcd->pData);
286 }
287 
293 void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
294 {
295  USBD_LL_Resume(hpcd->pData);
296 }
297 
304 void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
305 {
306  USBD_LL_IsoOUTIncomplete(hpcd->pData, epnum);
307 }
308 
315 void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
316 {
317  USBD_LL_IsoINIncomplete(hpcd->pData, epnum);
318 }
319 
325 void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
326 {
327  USBD_LL_DevConnected(hpcd->pData);
328 }
329 
335 void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
336 {
337  USBD_LL_DevDisconnected(hpcd->pData);
338 }
339 
340 /*******************************************************************************
341  LL Driver Interface (USB Device Library --> PCD)
342 *******************************************************************************/
348 USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev)
349 {
350  /* Change Systick prioity */
351  NVIC_SetPriority (SysTick_IRQn, 0);
352 
353 #ifdef USE_USB_FS
354  /*Set LL Driver parameters */
355  hpcd.Instance = USB_OTG_FS;
356  hpcd.Init.dev_endpoints = 4;
357  hpcd.Init.use_dedicated_ep1 = 0;
358  hpcd.Init.ep0_mps = 0x40;
359  hpcd.Init.dma_enable = 0;
360  hpcd.Init.low_power_enable = 0;
361  hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
362  hpcd.Init.Sof_enable = 0;
363  hpcd.Init.speed = PCD_SPEED_FULL;
364  hpcd.Init.vbus_sensing_enable = DISABLE;
365  /* Link The driver to the stack */
366  hpcd.pData = pdev;
367  pdev->pData = &hpcd;
368  /*Initialize LL Driver */
369  HAL_PCD_Init(&hpcd);
370 
371  HAL_PCDEx_SetRxFiFo(&hpcd, 0x80);
372  HAL_PCDEx_SetTxFiFo(&hpcd, 0, 0x40);
373  HAL_PCDEx_SetTxFiFo(&hpcd, 1, 0x80);
374 
375 
376 #endif
377 #ifdef USE_USB_HS
378  /*Set LL Driver parameters */
379  hpcd.Instance = USB_OTG_HS;
380  hpcd.Init.dev_endpoints = 6;
381  hpcd.Init.use_dedicated_ep1 = 0;
382  hpcd.Init.ep0_mps = 0x40;
383 
384  /* Be aware that enabling USB-DMA mode will result in data being sent only by
385  multiple of 4 packet sizes. This is due to the fact that USB DMA does
386  not allow sending data from non word-aligned addresses.
387  For this specific application, it is advised to not enable this option
388  unless required. */
389  hpcd.Init.dma_enable = 0;
390 
391  hpcd.Init.low_power_enable = 0;
392 
393 #ifdef USE_USB_HS_IN_FS
394  hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
395  hpcd.Init.speed = PCD_SPEED_HIGH_IN_FULL;
396 #else
397  hpcd.Init.phy_itface = PCD_PHY_ULPI;
398  hpcd.Init.speed = PCD_SPEED_HIGH;
399 #endif
400  hpcd.Init.Sof_enable = 0;
401  hpcd.Init.vbus_sensing_enable = DISABLE;
402  /* Link The driver to the stack */
403  hpcd.pData = pdev;
404  pdev->pData = &hpcd;
405  /*Initialize LL Driver */
406  HAL_PCD_Init(&hpcd);
407 
408  HAL_PCDEx_SetRxFiFo(&hpcd, 0x200);
409  HAL_PCDEx_SetTxFiFo(&hpcd, 0, 0x80);
410  HAL_PCDEx_SetTxFiFo(&hpcd, 1, 0x174);
411 
412 
413 #endif
414  return USBD_OK;
415 }
416 
422 USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev)
423 {
424  HAL_PCD_DeInit(pdev->pData);
425  return USBD_OK;
426 }
427 
433 USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev)
434 {
435  HAL_PCD_Start(pdev->pData);
436  return USBD_OK;
437 }
438 
444 USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev)
445 {
446  HAL_PCD_Stop(pdev->pData);
447  return USBD_OK;
448 }
449 
458 USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev,
459  uint8_t ep_addr,
460  uint8_t ep_type,
461  uint16_t ep_mps)
462 {
463  HAL_PCD_EP_Open(pdev->pData, ep_addr, ep_mps, ep_type);
464  return USBD_OK;
465 }
466 
473 USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
474 {
475  HAL_PCD_EP_Close(pdev->pData, ep_addr);
476  return USBD_OK;
477 }
478 
485 USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
486 {
487  HAL_PCD_EP_Flush(pdev->pData, ep_addr);
488  return USBD_OK;
489 }
490 
497 USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
498 {
499  HAL_PCD_EP_SetStall(pdev->pData, ep_addr);
500  return USBD_OK;
501 }
502 
509 USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
510 {
511  HAL_PCD_EP_ClrStall(pdev->pData, ep_addr);
512  return USBD_OK;
513 }
514 
521 uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
522 {
523  PCD_HandleTypeDef *hpcd = pdev->pData;
524 
525  if((ep_addr & 0x80) == 0x80)
526  {
527  return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
528  }
529  else
530  {
531  return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
532  }
533 }
534 
541 USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr)
542 {
543  HAL_PCD_SetAddress(pdev->pData, dev_addr);
544  return USBD_OK;
545 }
546 
555 USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev,
556  uint8_t ep_addr,
557  uint8_t *pbuf,
558  uint16_t size)
559 {
560  HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size);
561  return USBD_OK;
562 }
563 
572 USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev,
573  uint8_t ep_addr,
574  uint8_t *pbuf,
575  uint16_t size)
576 {
577  HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);
578 }
579 
586 uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
587 {
588  return HAL_PCD_EP_GetRxCount(pdev->pData, ep_addr);
589 }
590 
596 void USBD_LL_Delay(uint32_t Delay)
597 {
598  HAL_Delay(Delay);
599 }
600 
601 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/